package com.example.demo.controller;

import cn.hutool.core.date.DateTime;

import com.example.demo.entity.Dakayonghu;
import com.example.demo.entity.Querybyaccountandmac;
import com.example.demo.entity.Querydakamingxiandbydatebymac;
import com.example.demo.entity.RequestBodEntity;
import com.example.demo.service.DakamingxiService;
import com.example.demo.service.DakayonghuService;
import com.example.demo.util.DateUtil;
import com.example.demo.util.LoginAuthUtil;
import com.example.demo.util.StringUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;



import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 数据展示
 */
@Api(value = "dataShow ", description = "打卡控制器")
@Controller
public class DataShowController {
    private static final Logger LOG = LoggerFactory.getLogger(LoginAuthUtil.class);
    @Autowired
    private DakamingxiService dakamingxiService;
    @Autowired
    private DakayonghuService dakayonghuService;

    /**
     * 根据天显示数据
     * @return
     */
    @ApiOperation(value = "数据展示 DAY")
    @RequestMapping( value = "/CloudEco/QueryInfo/byDate",method = RequestMethod.POST )
    @ResponseBody
    public Map dataShows(@RequestBody RequestBodEntity requestBodEntity){
        System.out.println(requestBodEntity.toString());
        System.out.println(requestBodEntity.getMac());
        System.out.println(requestBodEntity.getDate());
        System.out.println(requestBodEntity.getSvcID());
        Map map=new HashMap();
        List<Querydakamingxiandbydatebymac> querybyaccountandmacList=null;
        if (!requestBodEntity.getMac().equals("")) {
            Dakayonghu dakayonghu = dakayonghuService.queryByMac(requestBodEntity.getMac());
            if (dakayonghu.getPrivilege() == null || dakayonghu.getPrivilege().equals("0")) {
                //无权限
                map.put("errcode", "1");
                map.put("errmsg", "没有权限查看");
            } else {
                if (dakayonghu.getPrivilege().equals("1")) {
                    //权限为 1 查询自己
                    querybyaccountandmacList = dakamingxiService.queryDakamingxiandbydatebymac(requestBodEntity.getMac(), requestBodEntity.getDate());

                } else {
                    //权限为 2 查询所有
                    querybyaccountandmacList = dakamingxiService.queryDakamingxiandbydatebymac(requestBodEntity.getDate());
                }
                querybyaccountandmacList.stream().forEach(querydakamingxiandbydatebymac -> {
                    if (querydakamingxiandbydatebymac.getUname()==null){
                        String Uname=  StringUtils.getStarString(querydakamingxiandbydatebymac.getMac().trim().replace("-",""),2,10);
                        querydakamingxiandbydatebymac.setUname(Uname);}
                });
                map.put("errcode","0");
                map.put("errmsg","");
            }
            map.put("data", querybyaccountandmacList);
        }
        return map;
    }

    /**
     * 根据周显示数据
     * @return
     */
    @ApiOperation(value = "数据展示 WEEK")
    @RequestMapping( value = "/CloudEco/QueryInfo/byWeek",method = RequestMethod.POST )
    @ResponseBody
    public Map byWeeks(@RequestBody RequestBodEntity requestBodEntity){
        Map map=new HashMap();
        List<Querydakamingxiandbydatebymac> querybyaccountandmacList=null;
        if (!requestBodEntity.getMac().equals("")) {
            Dakayonghu dakayonghu = dakayonghuService.queryByMac(requestBodEntity.getMac());
            if (dakayonghu.getPrivilege() == null || dakayonghu.getPrivilege().equals("0")) {
                //无权限
                map.put("errcode", "1");
                map.put("errmsg", "没有权限查看");
            } else {
//                if (dakayonghu.getPrivilege().equals("1")) {
//                    //权限为 1 查询自己
//                    querybyaccountandmacList = dakamingxiService.queryDakamingxiandbyWeekbymac(account,mac, startDate,endDate);
//
//                } else {
                //权限为 2 查询所有
                //querybyaccountandmacList = dakamingxiService.queryDakamingxiandbyWeek(startDate,endDate);
                querybyaccountandmacList = dakamingxiService.queryDakamingxiandbyWeekbymac(requestBodEntity.getAccount(),requestBodEntity.getMac(), requestBodEntity.getStartDate(),requestBodEntity.getEndDate());
                querybyaccountandmacList.stream().forEach(querydakamingxiandbydatebymac -> {
                    if (querydakamingxiandbydatebymac.getUname()==null){
                        String Uname=  StringUtils.getStarString(querydakamingxiandbydatebymac.getMac().trim().replace("-",""),2,10);
                        querydakamingxiandbydatebymac.setUname(Uname);
                    }
                    querydakamingxiandbydatebymac.setDayOfWeek(DateUtil.dateToWeek(querydakamingxiandbydatebymac.getcDate().toString()));
                });
                // }
                map.put("errcode","0");
                map.put("errmsg","");
            }
            map.put("data", querybyaccountandmacList);
        }
        return map;
    }


    /**
     *下载，根据周 或者天下载数据
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "download DAY")
    @RequestMapping(value = "/CloudEco/QueryInfo/download", method = RequestMethod.GET)
    public void exportE(HttpServletResponse response,@RequestParam (value = "uId" ,required=false) String uId,
                        @RequestParam (value = "endDate" ,required=false) String endDate,
                        @RequestParam (value = "mac" ,required=false) String mac,
                        @RequestParam (value = "SvcID" ,required=false) String SvcID,
                        @RequestParam (value = "startDate" ,required=false) String startDate,
                        @RequestParam (value = "account" ,required=false) String account,
                        @RequestParam (value = "departName" ,required=false) String departName) throws IOException {

        byte[] data = dakamingxiService.exportDaKaMingXi(uId,endDate,mac,SvcID,startDate,account);
        response.reset();
        String fileName="";
        if (account.equals("0")){
            fileName =startDate+"~"+endDate + "打卡记录" + ".xls";
        }else {
            fileName =startDate+"~"+endDate +account+"打卡记录" + ".xls";
        }
        response.setContentType("application/octet-stream; charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8"));
        response.addHeader("Content-Length", "" + data.length);
        IOUtils.write(data,response.getOutputStream());
    }
}
